# 2.3 configRoute

# 1. Common Configurations

This method is used to configure access routes. The following code maps the path "/hello" to the HelloController. With this configuration, accessing http://localhost/hello will call the HelloController.index() method, while http://localhost/hello/methodName will call the HelloController.methodName() method.

public void configRoute(Routes me) {
    // If you want to map public methods in the controller superclass to action, configure it to true, but it's generally not necessary.
    me.setMappingSuperClass(false);
    
    // Configure baseViewPath to allow the render(...) method to omit the baseViewPath prefix.
    me.setBaseViewPath("/view");
    
    // Configure interceptors that apply to all Controllers registered in this Routes object.
    me.addInterceptor(new FrontInterceptor());
    
    // Route scanning, a feature added in jfinal 4.9.03. The parameter "com.xxx." specifies the package to scan.
    // Scanning only occurs within this package and its sub-packages.
    me.scan("com.xxx.");
    
    // Manually add routes. Note: If you use route scanning, do not manually add routes. Choose one or the other.
    me.add("/hello", HelloController.class);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

The Routes.setBaseViewPath(baseViewPath) method sets the base path for view rendering for all Controllers within this Routes. This base path, combined with the viewPath parameter passed to the Routes.add(...) method and the view parameter passed to the Controller.render(view) method, determines the final view path according to the following rule:

finalView = baseViewPath + viewPath + view
1

Note: If the view starts with the "/" character, it's an absolute path, and both baseViewPath and viewPath will be ignored.

# 2. Route Scanning (new feature in jfinal 4.9.03)

Starting from jfinal 4.9.03, route scanning was introduced. For this feature, the @Path annotation must be used in the Controller declaration, e.g.:

@Path("/project")
public class ProjectController extends Controller {
   ...
}
 
// The following code configures a viewPath for the controller.
@Path(value="/", viewPath="/index")
public class IndexController extends Controller {
   ...
}
1
2
3
4
5
6
7
8
9
10

The @Path annotation can configure both controllerPath and viewPath parameters. If viewPath is omitted, it defaults to the value of the first parameter. The two parameters of the @Path annotation are equivalent to the first and third parameters of the Routes.add(controllerPath, controllerClass, viewPath) method.

After using the @Path annotation, route scanning can be enabled with simple configuration:

public void configRoute(Routes me) {
   me.scan("com.xxx.");
}
1
2
3

By using the scan(...) method, route scanning is activated. In the code above, the parameter "com.xxx." means that only the "com.xxx." package and its sub-packages will be scanned.

If you need to categorize routes, set baseViewPath categorically, or configure interceptors at the Routes level categorically, you can refer to the configuration used in the jfinal club project as shown above.

Starting from the jfinal 3.6 version, a new configuration method was introduced:

public void configRoute(Routes me) {
  me.setMappingSuperClass(false);
}
1
2
3

This method determines whether public methods in the controller superclass should be mapped as actions. By default, this is set to false, meaning methods in the superclass won't become actions.

Note: This configuration was introduced in version 3.6, so older jfinal projects that need superclass methods as actions should set this to true.

This configuration is essentially a performance optimization to speed up project startup. If Routes is split into multiple sub-Routes, it's recommended to configure this in the sub-Routes that need it, as it can work independently within sub-Routes. Other Routes without this configuration can still benefit from this performance optimization.

Last Updated: 9/17/2023, 5:34:57 AM